﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Model;
using BLL;

public partial class MembershipCard_BuyUserList : BasePage
{
    CardToUseBO cbo = new CardToUseBO();
    public string gid = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (GetQeuryString("para") == "GetList")
        {
            BindData();
        }
        else if (GetQeuryString("para") == "delete")
        {
            try
            {
                Response.Clear();
                string mid = GetQeuryString("mid");
                string cid = GetQeuryString("cid");
                CardToUse c = cbo.GetModel(int.Parse(mid),int.Parse(cid));
                if (c != null)
                {
                    Response.Write("已经使用，无法再次使用");
                }
                else
                {
                    c = new CardToUse();
                    c.CID = int.Parse(cid);
                    c.CreateDate = DateTime.Now;
                    c.MID = int.Parse(mid);
                    cbo.Add(c);
                    Response.Write("ok");
                }
                //Response.Write("测试");

            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
            finally
            {
                Response.End();
            }

        }
        //else if (GetQeuryString("para") == "delete2")
        //{
        //    try
        //    {
        //        Response.Clear();
        //        string id = GetQeuryString("id");
        //        GroupBuyingUser g = mbo.GetModel(int.Parse(id));
        //        if (g != null)
        //        {
        //            GroupBuying b = gbo.GetModel(int.Parse(g.GID.ToString()));
        //            b.Places = b.Places + g.Num;
        //            b.Purchases--;
        //            gbo.Update(b);
        //            mbo.Delete(g.ID);
        //        }
        //        Response.Write("ok");
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("Error" + ex.Message);
        //    }
        //    finally
        //    {
        //        Response.End();
        //    }

        //}
        else if (Request.QueryString["id"] != null)
        {
            gid = Request.QueryString["id"].ToString();
        }

    }

    private void BindData()
    {
        Response.Clear();
        //string s = "{\"total\":\"239\",\"rows\":[  {\"code\":\"001\",\"name\":\"Name 1\",\"addr\":\"Address 11\",\"addr\":\"col4 data\"}   ]}  ";
        int PageNumber = Convert.ToInt32(Request.Params["page"] == null ? "1" : Request.Params["page"]) - 1;
        int pageSize = Convert.ToInt32(Request.Params["rows"] == null ? "10" : Request.Params["rows"]);
        string sort = " order by " + (Request.Params["sort"] == null ? "id" : Request.Params["sort"]) + " " + (Request.Params["order"] == null ? " desc " : Request.Params["order"]);
        string condition = " ";
        string uid = txtUID.Text;// Request.Params["txtUID"] == null ? "" : Request.Params["txtUID"];
        string username = txtUserName.Text;// Request.Params["UserName"] == null ? "" : Request.Params["UserName"];
        if (uid.Trim() != "")
        {
            condition += " and uid like '%" + uid.Trim() + "%' ";
        }
        if (username.Trim() != "")
        {
            condition += " and username like '%" + username.Trim() + "%' ";
        }
        if (Request.QueryString["id"] != null)
        {
            condition += " u.MID=" + Request.QueryString["id"].ToString();
        }
        string con = " s.id="+CurrentUserInfo.MID;
        DataSet ds = access.GetCardToUse(sort, pageSize, PageNumber, condition,con);
        string re = ToJson.DataTableToJson(ds.Tables[0].Rows[0][0].ToString(), "rows", ds.Tables[1]);
        Response.Write(re);
        Response.End();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
}